var BSTMG = BSTMG || {};
BSTMG.invoice = {
		init: function(){
			var that = this;
			/*发票列表*/
			that.invoiceListReq();
			/* 邮寄发票 */
			that.addDelivery();
		},
		addDelivery: function(){
			var $update = $("#update");
			$update.on("click",function(){
				var html = '<div id="delivery"><form class="form-horizontal" role="form" style="margin-top:26px;"><div class="form-group"><label for="no" class="col-sm-3 control-label" style="width:100px;">快递类型：</label><div class="col-sm-8"><input type="text" class="form-control" id="deliveryType" placeholder="快递类型"></div></div><div class="form-group"><label for="deliveryNum" class="col-sm-3 control-label" style="width:100px;">快递编号：</label><div class="col-sm-8"><input type="text" class="form-control" id="deliveryNum" placeholder="快递编号"></div></div></form></div>';
				var rule = BSTYUN.dialog.creat({
					id: "addDelivery",
					content: html,
					fixed : false,
					lock: false,
					button: [{
						name: "确定",
						callback: function(){
							var invoiceId = $('#invoiceId').val(),
							deliveryType = $('#deliveryType').val(),
							deliveryNum = $('#deliveryNum').val()
							if(deliveryType == null ||  deliveryType == ''){
								BSTYUN.dialog.warn("快递类型不能为空！");
								return false;
							}else if(deliveryNum == null || deliveryNum == ''){
								BSTYUN.dialog.warn("快递编号不能为空！");
								return false;
							}
							$.ajax({
								type : "POST",
								url : "/sysAdmin/operate/invoice/updateInvoice",
								async : false,
								data:{
									invoiceId:invoiceId,
									deliveryType : deliveryType,
									deliveryNum : deliveryNum
								},
								success:function(data){
									if(data) {
										BSTYUN.dialog.ok("快递添加成功！",function(){
											location.href = "/sysAdmin/invoice/invoiceDetail/"+invoiceId;
										});
									}else {
										BSTYUN.dialog.warn("快递添加失败！");
									}
								}
							})
						},
						focus: true
					},{
						name: "取消",
						callback: function(){
							//alert("取消回调函数")
						},
						focus: false
					}]
				});
			});
		},
		invoiceListReq: function(){
			var url = "/sysAdmin/invoice/invoiceList/search";
			BSTMG.common.hjhPages(url,function(data){
				var invoiceList = data.invoiceList;
				var $tbody = $("#invoiceList tbody");
				var $tr = '';
				if(invoiceList.length > 0) {
					for(var i=0;i<invoiceList.length;i++){
						var invoice = invoiceList[i];
						var userName = invoice.userEmail;
						if(userName==null || userName==''){
							userName = invoice.userTel;
						}
						$tr += '<tr class="odd">';
						$tr += '<td data-id="'+invoice.invoiceId+'">'+(i+1)+'</td>';
						$tr += '<td>'+ BSTMG.tools.checkNull(userName)+'</td>';
						$tr += '<td>'+ BSTMG.tools.checkNull(invoice.invoiceTitle)+'</td>';
						$tr += '<td>'+ BSTMG.tools.checkNull(parseFloat(invoice.invoiceMoney).toFixed(2))+'</td>';
						$tr += '<td>'+ BSTMG.tools.checkNull(setOpenType(invoice.openType))  + '</td>';
						$tr += '<td>'+ BSTMG.tools.checkNull(setInvoiceType(invoice.invoiceType))+'</td>';
						$tr += '<td>'+ BSTMG.tools.checkNull(toDate(invoice.addTime,"yyyy-MM-dd hh:mm:ss"))+'</td>';
						$tr += '<td>'+ BSTMG.tools.checkNull(setInvoiceState(invoice.invoiceState))+'</td>';
						$tr += '<td><a type="button"  class="btn btn-primary" href="/sysAdmin/invoice/invoiceDetail/'+invoice.invoiceId+'">详情</a></td>';
						$tr += '</tr>';
					}
					$tbody.html($tr);
				}else {
					var $tr = '<tr class="odd"><td colspan="9">暂无数据！</td></tr>';
					$tbody.html($tr);
				}
			});
		},
}
$(function(){
	BSTMG.invoice.init();
});


//设置开具
function setOpenType(para){
	if (para == "0") {
		return "个人";
	} else if (para == "1") {
		return "企业";
	}
}
//设置发票类型
function setInvoiceType(para){
	if (para == "0") {
		return "增值税普通发票";
	} else if (para == "1") {
		return "增值税专用发票";
	} 
}
//设置发票状态
function setInvoiceState(para){
	if (para == "0") {
		return "未索取";
	} else if (para == "1") {
		return "待邮寄";
	} else if (para == "2") {
		return "已邮寄";
	}
}